﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.Estate;
using System.Data;

namespace ZLDC.Estate.TM
{
    public partial class CuserList : _PageBase
    {
        private int deptId;
        private string IdList;
        private string condition;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Page.Request.QueryString["tid"] != null || Page.Request.QueryString["tid"].Trim() != string.Empty)//获取部门ID
                {
                    ViewState["t_id"] = _Convert.ToInt(Page.Request.QueryString["tid"], 0);
                }

                deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);//获取登录人的部门ID

                IdList = CacheUtility.Cache_GetAllChildDept(deptId.ToString());


                // 获取文员所管辖的部门 （分行行政经理,分行行政经理助理,区域行政经理,区域行政经理助理）
                string deptNameList = "分行行政经理,分行行政经理助理,区域行政经理,区域行政经理助理,业务主任,分行经理,区域经理,分区经理,分行经理,营运总监,营运副总监,营业副总经理,营运总经理,营业总经理";
                if (deptNameList.Contains(_Utility.GetInfoBySession().m_sStation))
                {
                  string PosId=ZLDC.Common.BaseUser.UserInfo.GetManagePosDeptList(_Convert.ToInt(_Utility.GetInfoBySession().m_sEHRUserID.ToString()));
                  IdList = IdList + "," +PosId;
                }
                
                Session["IdList"] = IdList;

                if (_Convert.ToInt(ViewState["t_id"].ToString(), 0) > 0)
                {
                    deptId = _Convert.ToInt(ViewState["t_id"].ToString(), 0);
                }

                Session["deptId"] = deptId;

                // 判断是否有权限查看该本部门
                if (IdList.Contains(deptId.ToString()))
                    ViewState["t_id"] = "0";


                ltlDeptName.Text = ZLDC.DAL.Estate.DeptDAL.GetDeptNameById(deptId);//部门名称
                condition = " and u.t_id =" + deptId + "";

                string userName;
                string userNo;
                _Utility.GetUserId(txtUserName.Text.Trim(), out userName, out userNo);
                gvArea.DataSource = ZLDC.DAL.Estate.UserDAL.GetUserList(userName, deptId.ToString(), IdList, ViewState["t_id"].ToString(), condition);//绑定用户
                gvArea.DataBind();

                if (gvArea.Rows.Count > 0)
                {
                    btnSave.Visible = true;
                }
                else
                {
                    btnSave.Visible = false;
                }

                if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("还原密码"))
                {
                    pwd.Value = "1";
                }
            }
        }

        protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

                //----帐号禁用------------------
                System.Web.UI.WebControls.CheckBox chk = null;
                chk = (System.Web.UI.WebControls.CheckBox)e.Row.FindControl("cbxDisd");
                string dltflag =DataBinder.GetPropertyValue(e.Row.DataItem, "disabled").ToString();
                if (dltflag.Trim() == "True")
                {
                    chk.Checked = true;
                }

               
               
            }
          
        }
        // 用户管理分页
        protected void gvArea_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            string userName;
            string userNo;
            _Utility.GetUserId(txtUserName.Text.Trim(), out userName, out userNo);
            condition = " and u.t_id =" + Session["deptId"].ToString() + "";
            gvArea.PageIndex = e.NewPageIndex;
            gvArea.DataSource = ZLDC.DAL.Estate.UserDAL.GetUserList(userName, Session["deptId"].ToString(), Session["IdList"].ToString(), ViewState["t_id"].ToString(), condition);//绑定用户
            gvArea.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            System.Web.UI.WebControls.CheckBox chk = null;
            System.Web.UI.WebControls.Label lbl = null;
            string sValue = "0";

            for (int i = 0; i <gvArea.Rows.Count; i++)
            {
                chk = (System.Web.UI.WebControls.CheckBox)gvArea.Rows[i].FindControl("cbxDisd");
                lbl = (System.Web.UI.WebControls.Label)gvArea.Rows[i].FindControl("lblID");
                if (chk.Checked)
                {
                    sValue = "1";
                    ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("t_user", _Convert.ToInt(lbl.Text), 5, "禁用用户");

                }
                else
                {
                    sValue = "0";
                }
                sbSql.Append(" if(exists(select tmp_id from t_user where tmp_id = " + Convert.ToInt32(lbl.Text) + "))");
                sbSql.Append(" begin");
                sbSql.Append(" update t_user set dltflag = " + sValue + "  where tmp_id = " + Convert.ToInt32(lbl.Text) + ";");
                sbSql.Append(" end;");



            }

           
            if (sbSql.ToString() != string.Empty)
            {
                
                using (_Database db = new _Database("estate"))
                {
                 db.ExecuteSql(sbSql.ToString());
                }

                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('更新成功')</script>");
            }
            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string userNameList = txtUserName.Text;
            if (userNameList == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('请输入用户名称')</script>");
                return;
            }
           
                string userName = string.Empty; // 姓名
                string userNo = string.Empty;   // 工号
                _Utility.GetUserId(userNameList.Trim(), out userName, out userNo);
                condition= "";
                gvArea.DataSource = ZLDC.DAL.Estate.UserDAL.GetUserList(userName, Session["deptId"].ToString(), Session["IdList"].ToString(), ViewState["t_id"].ToString(),condition);//绑定用户
                gvArea.DataBind();
           
           

         
        }
    }
}
